package luj.tool.jnproxyan.proxy.network.socks.cmd.connect2;

import luj.bean.api.bean.Bean;
import luj.net.api.connection.NetDisconnectListener;
import luj.tool.jnproxyan.proxy.network.socks.actor.root.message.request.disconnect.P2sDisconnectMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkState;

@Deprecated
final class P2sDisconnect implements NetDisconnectListener {

  P2sDisconnect(ReqInfo req) {
    _req = req;
  }

  /**
   * @see luj.tool.jnproxyan.proxy.network.socks.actor.root.message.request.disconnect.OnP2sDisconnect#onHandle
   */
  @Override
  public void onDisconnect(Context ctx) {
    checkState(ctx.getConnectionState() == null);
    Long reqId = _req._reqId;
    LOG.debug("网站断开：{}", reqId);

    Bean<P2sDisconnectMsg> msg = _req._lujbean.createBean(P2sDisconnectMsg.class);
    msg.setField(m -> m::reqId, reqId);

    _req._socksRef.tell(msg.getValueInstance());
  }

  private static final Logger LOG = LoggerFactory.getLogger(P2sDisconnect.class);

  private final ReqInfo _req;
}
